<template>
    <div class="fillContainer">
        <div>
            <el-form
                    :inline="true"
                    ref="search_data"
            >

                <el-form-item>
                    <el-input v-model="search_data.keywords" clearable placeholder="请输入姓名/手机号/邮箱"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="search_data.status" clearable placeholder="状态">
                        <el-option
                                v-for="item in options"
                                :key="item.status"
                                :label="item.label"
                                :value="item.status">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-select v-model="search_data.sex" clearable placeholder="性别">
                        <el-option
                                v-for="item in sex"
                                :key="item.status"
                                :label="item.label"
                                :value="item.status">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="">
                    <el-date-picker
                            v-model="search_data.startTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择开始时间">
                    </el-date-picker>
                    --
                    <el-date-picker
                            v-model="search_data.endTime"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择结束时间">
                    </el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" size="small" icon="search" @click='searchData()'>查询</el-button>
                </el-form-item>

                <el-form-item class="btnRight">
                    <el-button type="primary" size="small" icon="view" @click='add()'>新增</el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table
                :data="tableData"
                style="width: 100%" border>
            <el-table-column
                    type="index"
                    align="center"
                    label="序号"
                    width="70px">
            </el-table-column>

            <el-table-column
                    label="用户名"
                    prop="username"
                    align="center">
            </el-table-column>

            <el-table-column
                    label="邮箱"
                    prop="email"
                    align="center">
            </el-table-column>

            <el-table-column
                    label="手机号"
                    prop="phone"
                    align="center">
            </el-table-column>

            <el-table-column
                    label="状态"
                    prop="status"
                    align="center">
                <template slot-scope="scope">
                    {{ scope.row.status == 1 ?"正常":"锁定" }}
                </template>
            </el-table-column>

            <el-table-column
                    label="性别"
                    prop="sex"
                    align="center">
                <template slot-scope="scope">
                    {{ scope.row.sex == 1 ?"男":(scope.row.sex == 2 ? "女" : "保密") }}
                </template>
            </el-table-column>

            <el-table-column
                    label="创建时间"
                    prop="createTime"
                    align="center">
                <template slot-scope="scope">
                    <i class="el-icon-time"></i>
                    <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
                </template>
            </el-table-column>

            <el-table-column align="center" label="操作" prop="operation">
                <template slot-scope="scope">
                    <el-button
                            type="warning"
                            size="small"
                            icon="edit"
                            @click="handleEdit(scope.$index, scope.row)">编辑
                    </el-button>
                    <el-button
                            size="small"
                            type="danger"
                            icon="delete"
                            @click="handleDelete(scope.$index, scope.row)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <Pagination :paginations="paginations" @updatePageSize="updatePageSize"
                    @updatePageNo="updatePageNo"></Pagination>

        <Dialog :dialog="dialog" @update="getData" :fromData="fromData"></Dialog>
    </div>
</template>

<script>
    import ElTableBody from "element-ui";
    import Dialog from '@/components/admin/UserDialog';
    import Pagination from '@/components/Pagination';

    export default {
        name: "User",
        components: {ElTableBody, Dialog, Pagination},
        data() {
            return {
                tableData: [],
                fromData: {
                    username: '',
                    password: '',
                    phone: '',
                    email: '',
                    status: '',
                    type: '',
                    sex: ''
                },
                dialog: {
                    show: false,
                    title: '',   //标题
                    option: 'edit',  //操作
                },
                //需要给分页组件传的信息
                paginations: {
                    page_index: 1, // 当前位于哪页
                    total: 0, // 总数
                    page_size: 20, // 1页显示多少条
                    page_sizes: [20, 30, 40, 50], //每页显示多少条
                    layout: "total, sizes, prev, pager, next, jumper" // 翻页属性
                },
                search_data: {
                    startTime: "",
                    endTime: "",
                    keywords: "",
                    status: "",
                    sex: ""
                },
                options: [{
                    status: 1,
                    label: '正常'
                }, {
                    status: 2,
                    label: '锁定'
                }],
                sex: [{
                    status: 1,
                    label: '男'
                }, {
                    status: 2,
                    label: '女'
                }, {
                    status: 3,
                    label: '保密'
                }],
            }
        },
        created() {
            this.getData(this.paginations.page_index, this.paginations.page_size);
        },
        methods: {
            handleEdit(index, row) {
                this.dialog = {
                    show: true,
                    title: "编辑用户信息",
                    option: "edit"
                };
                this.fromData = {
                    username: row.username,
                    phone: row.phone,
                    email: row.email,
                    status: row.status,
                    sex: row.sex,
                    id: row.id
                };
            }
            ,
            handleDelete(index, row) {
                this.$confirm('此操作将永久删除, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("/api/user/" + row.id).then(res => {
                        if (res.data.code === 0) {
                            this.getData();
                        }
                    });
                }, () => {
                    // 点击取消
                })
            },
            getData() {
                let pageIndex = this.paginations.page_index;
                let pageSize = this.paginations.page_size;
                this.$axios.get("/api/user/" + pageIndex + "/" + pageSize, {params: this.search_data}).then(res => {
                    // 登录成功
                    console.log(res.data);
                    this.paginations.total = res.data.data.total;
                    this.tableData = res.data.data.list;
                });
            },
            add() {
                this.dialog = {
                    show: true,
                    title: "添加用户",
                    option: "add"
                };
                this.fromData = {
                    status: 1,
                    sex: 1
                };
            },
            searchData() {
                console.log(this.search_data);
                this.getData();
            },

            updatePageSize(pageSize) {
                this.paginations.page_size = pageSize;
                this.getData();
            },
            updatePageNo(pageNo) {
                this.paginations.page_index = pageNo;
                this.getData();
            },
        },
    }
</script>

<style scoped>
    .fillContainer {
        width: 100%;
        height: 100%;
        padding: 16px;
        box-sizing: border-box;
    }

    .btnRight {
        float: right;
    }

</style>
